package cn.tedu.jsd2210.csmall.product.controller;

import cn.tedu.jsd2210.csmall.product.pojo.dto.AlbumAddNewDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.AlbumUpdateDto;
import cn.tedu.jsd2210.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.jsd2210.csmall.product.service.IAlbumService;
import cn.tedu.jsd2210.csmall.product.web.JsonResult;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/albums")
@Api(tags="04.相册管理模块")
@Validated
public class AlbumController {
    @Autowired
    private IAlbumService albumService;
//http://localhost:9080/album/add-new?name=009
    @ApiOperation("添加相册")
    @PostMapping("/add-new")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AlbumAddNewDto albumAddNewDto){
            log.debug("开始处理添加相册的请求,参数:{}",albumAddNewDto);
            albumService.addNew(albumAddNewDto);
            return JsonResult.ok();
    }

    // http://localhost:9080/albums/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除相册")
    @ApiOperationSupport(order = 200)
//    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "相册ID",required = true,dataType = "long",example = "100")
//        ,@ApiImplicitParam(name="username",value = "测试多参数用户名",required = true,dataType = "string")
//    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id){
        log.debug("开始处理根据id删除相册的请求,参数:{}",id);
        albumService.delete(id);
        return JsonResult.ok();
    }
    //不用再try catch,只要出现异常,自动执行全局异常处理器类中方法
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改相册")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name="id",value = "相册ID",required = true,dataType = "long",example = "100")
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id,
                                     @Valid AlbumUpdateDto albumUpdateDto){
        log.debug("开始处理根据id{}修改相册的请求,新数据:{}",id,albumUpdateDto);
        albumService.updateInfoById(albumUpdateDto,id);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AlbumListItemVO>> list(){
        log.debug("开始处理根据查询相册列表的请求.无参数");
        List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(list);
//        JsonResult jsonResult = JsonResult.ok();
//        jsonResult.setData(list);
//        return jsonResult;
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查找相册")
    @ApiOperationSupport(order = 410)
    public JsonResult<AlbumStandardVO> getStandardById(
            @PathVariable @Range
                    (min = 1L,message = "请提交有效的ID值!")
                    Long id){
        log.debug("开始处理根据ID:{}查询相册列表的请求",id);
        AlbumStandardVO queryResult = albumService.getStandardById(id);
        return JsonResult.ok(queryResult);
//        JsonResult jsonResult = JsonResult.ok();
//        jsonResult.setData(list);
//        return jsonResult;
    }


    // http://localhost:9080/albums/rebuild-cache
    @GetMapping("/rebuild-cache")
    @ApiOperation("重建相册缓存")
    @ApiOperationSupport(order = 900)
    public JsonResult<Void> rebuildCache(){
        log.debug("开始处理【重建相册缓存】的请求，无参数");
        albumService.rebuildCache();
        return JsonResult.ok();
    }
}
